<template>
	<xn-form-container
		:title="formData.id ? '编辑模块' : '增加模块'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="所属项目" name="projectCode">
				<ProjectSelect v-model:value="formData.projectCode" />
			</a-form-item>
			<a-form-item label="所属月份" name="belongingMonth">
				<a-select
					v-model:value="formData.belongingMonth"
					placeholder="请选择所属月份"
					:options="monthOptions"
					allowClear
				/>
			</a-form-item>
			<a-form-item label="所属公司" name="deptId">
				<DeptSelect v-model:value="formData.deptId" />
			</a-form-item>
			<a-form-item label="批次号" name="batchNumber">
				<a-input v-model:value="formData.batchNumber" placeholder="请输入批次号" allow-clear />
			</a-form-item>
			<a-form-item label="人数" name="personnelCount">
				<a-input v-model:value="formData.personnelCount" placeholder="请输入人数" allow-clear />
			</a-form-item>
			<a-form-item label="案件数量" name="caseCount">
				<a-input v-model:value="formData.caseCount" placeholder="请输入案件数量" allow-clear />
			</a-form-item>
			<a-form-item label="案件总金额" name="totalArrearsAmount">
				<a-input v-model:value="formData.totalArrearsAmount" placeholder="请输入案件总金额" allow-clear />
			</a-form-item>
			<a-form-item label="月目标金额" name="monthlyTargetAmount">
				<a-input v-model:value="formData.monthlyTargetAmount" placeholder="请输入月目标金额" allow-clear />
			</a-form-item>
			<a-form-item label="结佣比例" name="commissionRatio">
				<a-input v-model:value="formData.commissionRatio" placeholder="请输入结佣比例" allow-clear />
			</a-form-item>
			<a-form-item label="佣金目标" name="commissionTarget">
				<a-input v-model:value="formData.commissionTarget" placeholder="请输入结佣比例" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelectorRef" @iconCallBack="iconCallBack" />
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import tool from '@/utils/tool'
	import monthlyApi from '@/api/bi/goals/monthly/monthlyApi'
	import bizUserApi from '@/api/biz/bizUserApi'
	import DeptSelect from '@/components/Tjzy/Dept/index.vue'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const iconSelectorRef = ref()
	// 表单数据
	const formData = ref({})

	const monthOptions = tool.dictList('month')
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {
				sortCode: 99,
				color: '#1677FF'
			}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	// 默认要校验的
	const formRules = {
		projectCode: [required('请选择所属项目')],
		deptId: [required('请选择所属公司')],
		belongingMonth: [required('请选择所属月份')],
		batchNumber: [required('请输入批次号')],
		personnelCount: [required('请输入人数')],
		caseCount: [required('请输入案件数量')],
		totalArrearsAmount: [required('请输入案件总金额')],
		monthlyTargetAmount: [required('请输月目标金额')],
		commissionRatio: [required('请输入结佣比例')],
		commissionTarget: [required('请输入佣金目标')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		console.log(formData.value)
		formRef.value
			.validate()
			.then(() => {
				console.log(formData.value)
				if (formData.value.id) {
					monthlyApi.edit(formData.value).then(() => {
						onClose()
						emit('successful')
					})
				} else {
					monthlyApi.add(formData.value).then(() => {
						onClose()
						emit('successful')
					})
				}
			})
			.catch(() => {})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
